import Vue from 'vue'

export function getItem(){
	
	return  JSON.parse(window.localStorage.getItem("cat") || "[]") ;
	
}

export function setItem(obj){
	
	let  Stringcat = getItem()
	Stringcat.push(obj) //[{}]
	
	window.localStorage.setItem("cat",JSON.stringify(Stringcat))
	
}

export function couterItem(id){
	
	let  Stringcat = getItem()
	for (let i = Stringcat.length-1 ; i >= 0; i--) {
		 if (Stringcat[i].goodsid == id){
		 	Stringcat[i].num == 1 ?  Stringcat.splice(i,1) : Stringcat[i].num--	
				break
	     	}
	}
	
	
	window.localStorage.setItem("cat",JSON.stringify(Stringcat))
	
}



export function delItem(id){
	
	let Stringcat = getItem()
	let newArr = []
	Stringcat.forEach(item=>{
	     	item.goodsid != id &&	newArr.push(item)
	   })
	window.localStorage.setItem("cat",JSON.stringify(newArr))
	
}